﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RclSystem
{
    public partial class PreReceiptDialog : UIEditForm
    {
        public PreReceiptDialog()
        {
            InitializeComponent();
            dt_receiptdate.Value = DateTime.Now;
            DataTable dt_saleman = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_saleman");
            cb_saleman.DataSource = dt_saleman;
            cb_saleman.DisplayMember = "name";
            cb_saleman.ValueMember = "id";
            cb_saleman.SelectedIndex = -1;
        }

        protected override bool CheckData()
        {
            return CheckEmpty(cb_company, "请输入单位名称") && CheckEmpty(cb_saleman, "请选择业务员") && CheckEmpty(cb_receiptway, "请选择收款方式") && CheckEmpty(dt_receiptdate, "请选择收款日期")
                 && CheckEmpty(txt_interestrate, "请输入利率") && CheckEmpty(txt_receiptamount, "请输入收款金额") && CheckEmpty(txt_overduedays, "请输入逾期天数")
                  && CheckEmpty(txt_interest, "请输入逾期利息");
        }

        private ReceiptEntity receipt;

        public ReceiptEntity ReceiptEntity
        {
            get
            {
                if (receipt == null)
                {
                    receipt = new ReceiptEntity();
                }

                receipt.salesman = cb_saleman.Text;
                receipt.companyname = cb_company.Text;
                receipt.receiptdate = dt_receiptdate.Value;
                receipt.receiptway = cb_receiptway.Text;
                receipt.receiptamount = decimal.Parse(txt_receiptamount.Text);
                receipt.interestrate = double.Parse(txt_interestrate.Text);
                receipt.interest = double.Parse(txt_interest.Text);
                receipt.overduedays = int.Parse(txt_overduedays.Text);
                return receipt;
            }

            set
            {
                receipt = value;
                cb_saleman.Text = value.salesman;
                cb_company.Text = value.companyname;
                dt_receiptdate.Value = value.receiptdate;
                cb_receiptway.Text = value.receiptway;
                txt_receiptamount.Text = value.receiptamount.ToString();
                txt_interestrate.Text = value.interestrate.ToString();
                txt_interest.Text = value.interest.ToString();
                txt_overduedays.Text = value.overduedays.ToString();
            }
        }

        private void cb_company_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cb_saleman_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_saleman.SelectedIndex != -1)
            {
                if (cb_saleman.SelectedValue.GetType() != typeof(DataRowView))
                {
                    DataTable dt_company = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where parentid = " + cb_saleman.SelectedValue.ToString());
                    cb_company.DataSource = dt_company;
                    cb_company.DisplayMember = "name";
                    cb_company.ValueMember = "id";
                }

            }
        }
    }
}
